<?php

namespace CtyunOss\config;

use Carbon\Carbon;
use CtyunOss\cors\ArrayObject;
use CtyunOss\exceptions\OssConfigException;

/**
 * 请求配置项
 * Class Request
 * @package CtyunOss\config
 *
 * @author  Violet_Ice紫冰 <violetice@aliyun.com>
 * @create  2020-06-24 19:36:19
 */
class Request extends ArrayObject
{
    /** @var string 请求方式 */
    private $method = 'GET';

    /** @var string 请求域名 */
    private $host;

    /** @var string 接口地址 */
    private $url;

    /** @var array 普通的请求头 */
    private $header = [];

    /** @var array x-amz请求头 */
    private $xHeader = [];

    /** @var string 容器名称 */
    private $bucket = '';

    /** @var array Url请求参数 */
    private $query = [];

    /** @var array Client配置 */
    private $options = [];

    /** @var Carbon 时间 */
    private $Date;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->Date = Carbon::now();
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * @param string $method
     */
    public function setMethod(string $method)
    {
        $this->method = $method;
    }

    /**
     * @return string
     */
    public function getHost(): string
    {
        return $this->host;
    }

    /**
     * @param string $host
     */
    public function setHost(string $host)
    {
        $this->host = $host;
    }

    /**
     * @return string
     */
    public function getUrl(): string
    {
        return $this->url;
    }

    /**
     * @param string $url
     */
    public function setUrl(string $url)
    {
        $this->url = $url;
    }

    /**
     * @param null $header
     *
     * @return array|mixed
     */
    public function getHeader($header = null)
    {
        return $header ? ($this->header[$header] ?? '') : $this->header;
    }

    /**
     * @param $key
     * @param null $header
     *
     * @throws OssConfigException
     */
    public function setHeader($key, $header = null)
    {
        if (is_array($key)) {
            $this->header = $key;
        }
        else if (is_string($key)) {
            $this->header[$key] = $header;
        }
        else {
            throw new OssConfigException('不支持的 setHeader 方式');
        }
    }

    /**
     * @return array
     */
    public function getXHeader(): array
    {
        return $this->xHeader;
    }

    /**
     * @param $key
     * @param $header
     *
     * @throws OssConfigException
     */
    public function setXHeader($key, $header = null)
    {
        if (is_array($key)) {
            $this->xHeader = $key;
        }
        else if (is_string($key)) {
            $this->xHeader[$key] = $header;
        }
        else {
            throw new OssConfigException('不支持的 setXHeader 方式');
        }
    }

    /**
     * @return string
     */
    public function getBucket(): string
    {
        return $this->bucket;
    }

    /**
     * @param string $bucket
     */
    public function setBucket(string $bucket)
    {
        $this->bucket = $bucket;
    }

    /**
     * @return array
     */
    public function getQuery(): array
    {
        return $this->query;
    }

    /**
     * @param $key
     * @param $query
     *
     * @throws OssConfigException
     */
    public function setQuery($key, $query = null)
    {
        if (is_array($key)) {
            $this->query = $key;
        }
        else if (is_string($key)) {
            $this->query[$key] = $query;
        }
        else {
            throw new OssConfigException('不支持的 setQuery 方式');
        }
    }

    /**
     * @return array
     */
    public function getOptions(): array
    {
        return $this->options;
    }

    /**
     * @param array $options
     */
    public function setOptions(array $options)
    {
        $this->options = $options;
    }

    /**
     * @return Carbon
     */
    public function getDate(): Carbon
    {
        return $this->Date;
    }
}